package py.edu.ucom.cadira.logic;

import java.io.IOException;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import py.edu.ucom.cadira.net.XMLSerializable;
import py.edu.ucom.cadira.net.serializables.CreateResponse;
import py.edu.ucom.cadira.net.serializables.JoinRequest;
import py.edu.ucom.cadira.net.serializables.JoinResponse;
import py.edu.ucom.cadira.net.serializables.MoveFichaRequest;
import py.edu.ucom.cadira.net.serializables.SetBoardRequest;
import py.edu.ucom.cadira.net.serializables.SetBoardResponse;
import py.edu.ucom.cadira.net.serializables.StandarRequest;
import py.edu.ucom.cadira.net.serializables.ListTableResponse;
import py.edu.ucom.cadira.net.serializables.LoginRequest;
import py.edu.ucom.cadira.net.serializables.LoginResponse;
import py.edu.ucom.cadira.net.serializables.StartGameRequest;
import py.edu.ucom.cadira.net.serializables.StartGameResponse;
import py.edu.ucom.cadira.xml.BadProtocolException;
import py.edu.ucom.cadira.xml.XMLSerializer;
import py.edu.ucom.cadira.game.event.war.*;

public class XmlTester {

    public static void testLoginRequest() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build("xmlSamples/login/cliente-login.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        LoginRequest lrequest = (LoginRequest) objLogin;
        Document xml = XMLSerializer.toXML(lrequest);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        LoginRequest lrequest2 = (LoginRequest) objLogin2;
        if (lrequest.equals(lrequest2)) {
            System.out.println("LOGIN ok\n");
        } else {
            System.out.println("LOGIN failed\n");
        }
    }
    public static void testLoginResponse() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build("xmlSamples/login/servidor-login.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        LoginResponse lresponse = (LoginResponse) objLogin;
        Document xml = XMLSerializer.toXML(lresponse);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        LoginResponse lresponse2 = (LoginResponse) objLogin2;
        if (lresponse.equals(lresponse2)) {
            System.out.println("STATUS true\n");
        } else {
            System.out.println("STATUS false\n");
        }
    }
    public static void testStandardRequest() throws JDOMException, IOException, BadProtocolException {
        StandarRequest request = new StandarRequest();
        request.setTypeRequest(StandarRequest.TYPE.LIST_TABLES);
        Document xml = XMLSerializer.toXML(request);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable request2 = XMLSerializer.fromXML(xml);
        if (request.equals(request2)) {
            System.out.println("ListTableReques OK\n");
        } else {
            System.out.println("ListTableReques failed\n");
        }
    }
    public static void testListTableResponse() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build("xmlSamples/listMesas/servidor-listMesas.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        ListTableResponse lMesas1 = (ListTableResponse) objLogin;
//        Mesa mesa = new Mesa(1);
//        mesa.setGameInterface(new WarGame());
//        mesa.addPlayer(new User("largo"));
//        mesa.addPlayer(new User("douglas"));
//        mesa.setWatchers(new User("Miron"));
//        lMesas1.addMesa(mesa);
        System.out.println("*********************************************");
        Document xml = XMLSerializer.toXML(lMesas1);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        ListTableResponse lMesas2 = (ListTableResponse) objLogin2;
        Document xml2 = XMLSerializer.toXML(lMesas2);
        XMLOutputter outputter2 = new XMLOutputter(Format.getPrettyFormat());
        outputter2.output(xml2, System.out);
        if (lMesas1.equals(lMesas2)) {
            System.out.println("ListTableResponse OK\n");
        } else {
            System.out.println("ListTableResponse failed\n");
        }
    }
    public static void testStartGameRequest() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build("xmlSamples/startGame/cliente-startGame.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        StartGameRequest startgamereq = (StartGameRequest) objLogin;
        Document xml = XMLSerializer.toXML(startgamereq);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        StartGameRequest startgamereq2 = (StartGameRequest) objLogin2;
        Document xml2 = XMLSerializer.toXML(startgamereq2);
        XMLOutputter outputter2 = new XMLOutputter(Format.getPrettyFormat());
        outputter2.output(xml2, System.out);
        if (startgamereq.equals(startgamereq2)) {
            System.out.println("StartGameRequest OK\n");
        } else {
            System.out.println("StartGameRequest failed");
        }
    }
    public static void testStartGameResponse() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/startGame/servidor-startGame.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        StartGameResponse startgameresp = (StartGameResponse) objLogin;
        Document xml = XMLSerializer.toXML(startgameresp);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        StartGameResponse startgameresp2 = (StartGameResponse) objLogin2;
        Document xml2 = XMLSerializer.toXML(startgameresp2);
        XMLOutputter outputter2 = new XMLOutputter(Format.getPrettyFormat());
        outputter2.output(xml2, System.out);
        if (startgameresp.equals(startgameresp2)) {
            System.out.println("StartGameResponse OK\n");
        } else {
            System.out.println("StartGameResponse failed");
        }
    }
    public static void testJoinRequest() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/joinTable/cliente-joinTable.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        JoinRequest jrequest = (JoinRequest) objLogin;
        Document xml = XMLSerializer.toXML(jrequest);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        JoinRequest jrequest2 = (JoinRequest) objLogin2;
        if (jrequest.equals(jrequest2)) {
            System.out.println("JoinRequest OK\n");
        } else {
            System.out.println("JoinRequest failed");
        }
    }
    public static void testJoinResponse() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/joinTable/servidor-joinTable.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        JoinResponse jresponse = (JoinResponse) objLogin;
        //jresponse.setMesa(new Mesa());
        // parche para evitar un NULLPOINTEREXCEPTION en el JoinResponse
        //jresponse.getMesa().setGameInterface(new WarGame());
        Document xml = XMLSerializer.toXML(jresponse);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        JoinResponse jresponse2 = (JoinResponse) objLogin2;
        // parche para evitar un NULLPOINTEREXCEPTION en el JoinResponse
        //jresponse2.getMesa().setGameInterface(new WarGame());
        Document xml2 = XMLSerializer.toXML(jresponse2);
        XMLOutputter outputter2 = new XMLOutputter(Format.getPrettyFormat());
        outputter2.output(xml2, System.out);
        if (jresponse.equals(jresponse2)) {
            System.out.println("JoinResponse OK\n");
        } else {
            System.out.println("JoinResponse failed");
        }
    }
    public static void testCreateResponse() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/createTable/servidor-createTable.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        CreateResponse cresponse = (CreateResponse) objLogin;
        Document xml = XMLSerializer.toXML(cresponse);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        CreateResponse cresponse2 = (CreateResponse) objLogin2;
        if (cresponse.equals(cresponse2)) {
            System.out.println("CreateResponse OK\n");
        } else {
            System.out.println("CreateResponse failed");
        }
    }

    public static void testSetBoardRequest() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/SetBoard/cliente-setBoard.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        SetBoardRequest sBoard = (SetBoardRequest) objLogin;
        Document xml = XMLSerializer.toXML(sBoard);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        SetBoardRequest sBoard2 = (SetBoardRequest) objLogin2;
        Document xml2 = XMLSerializer.toXML(sBoard2);
        XMLOutputter outputter2 = new XMLOutputter(Format.getPrettyFormat());
        outputter2.output(xml2, System.out);
        if (sBoard.equals(sBoard2)) {
            System.out.println("SetBoard OK");
        } else {
            System.out.println("SetBoard failed");
        }
    }
    public static void testSetBoardResponse() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/SetBoard/servidor-setBoard.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        SetBoardResponse sBoard = (SetBoardResponse) objLogin;
        Document xml = XMLSerializer.toXML(sBoard);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        SetBoardResponse sBoard2 = (SetBoardResponse) objLogin2;
        Document xml2 = XMLSerializer.toXML(sBoard2);
        XMLOutputter outputter2 = new XMLOutputter(Format.getPrettyFormat());
        outputter2.output(xml2, System.out);
        if (sBoard.equals(sBoard2)) {
            System.out.println("SetBoard OK");
        } else {
            System.out.println("SetBoard failed");
        }
    }
    public static void testMoveFichaRequest() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/moveFicha/cliente-moveFicha.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        MoveFichaRequest sBoard = (MoveFichaRequest) objLogin;
        Document xml = XMLSerializer.toXML(sBoard);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        MoveFichaRequest sBoard2 = (MoveFichaRequest) objLogin2;
        Document xml2 = XMLSerializer.toXML(sBoard2);
        XMLOutputter outputter2 = new XMLOutputter(Format.getPrettyFormat());
        outputter2.output(xml2, System.out);
        if (sBoard.equals(sBoard2)) {
            System.out.println("SetBoard OK");
        } else {
            System.out.println("SetBoard failed");
        }
    }
//    public static void testMoveFichaResponse() throws JDOMException, IOException, BadProtocolException {
//        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
//        Document doc = builder.build("xmlSamples/moveFicha/servidor-moveFicha.xml");
//        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
//        MoveFichaResponse sBoard = (MoveFichaResponse) objLogin;
//        Document xml = XMLSerializer.toXML(sBoard);
//        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
//        outputter.output(xml, System.out);
//        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
//        MoveFichaResponse sBoard2 = (MoveFichaResponse) objLogin2;
//        Document xml2 = XMLSerializer.toXML(sBoard2);
//        XMLOutputter outputter2 = new XMLOutputter(Format.getPrettyFormat());
//        outputter2.output(xml2, System.out);
//        if (sBoard.equals(sBoard2)) {
//            System.out.println("SetBoard OK");
//        } else {
//            System.out.println("SetBoard failed");
//        }
//    }   
    public static void testEndGameEvent() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/gameEventWar/cliente-EndGameEvent.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        EndGameEvent endgameevent = (EndGameEvent) objLogin;
        Document xml = XMLSerializer.toXML(endgameevent);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        EndGameEvent endgameevent2 = (EndGameEvent) objLogin2;
        if (endgameevent.equals(endgameevent2)) {
            System.out.println("EndGameEvent OK\n");
        } else {
            System.out.println("EndGameEvent failed");
        }
    }
    public static void testEndTimeEvent() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/gameEventWar/cliente-EndTimeEvent.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        EndTimeEvent endtimevent = (EndTimeEvent) objLogin;
        Document xml = XMLSerializer.toXML(endtimevent);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        EndTimeEvent endtimevent2 = (EndTimeEvent) objLogin2;
        if (endtimevent.equals(endtimevent2)) {
            System.out.println("EndTimeEvent OK\n");
        } else {
            System.out.println("EndTimeEvent failed");
        }
    }
    public static void testMoveTabEvent() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/gameEventWar/cliente-MoveTabEvent.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        MoveTabEvent movetabevent = (MoveTabEvent) objLogin;
        Document xml = XMLSerializer.toXML(movetabevent);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        MoveTabEvent movetabevent2 = (MoveTabEvent) objLogin2;
        if (movetabevent.equals(movetabevent2)) {
            System.out.println("MoveTabEvent OK\n");
        } else {
            System.out.println("MoveTabEvent failed");
        }
    }
    public static void testPlayUserEvent() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/gameEventWar/cliente-PlayUserEvent.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        PlayUserEvent playuserevent = (PlayUserEvent) objLogin;
        Document xml = XMLSerializer.toXML(playuserevent);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        PlayUserEvent playuserevent2 = (PlayUserEvent) objLogin2;
        if (playuserevent.equals(playuserevent2)) {
            System.out.println("PlayUserEvent OK\n");
        } else {
            System.out.println("PlayUserEvent failed");
        }
    }
    public static void testSetBoardEvent() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/gameEventWar/cliente-SetBoardEvent.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        SetBoardEvent setboardevent = (SetBoardEvent) objLogin;
        Document xml = XMLSerializer.toXML(setboardevent);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        SetBoardEvent setboardevent2 = (SetBoardEvent) objLogin2;
        if (setboardevent.equals(setboardevent2)) {
            System.out.println("SetBoardEvent OK\n");
        } else {
            System.out.println("SetBoardEvent failed");
        }
    }
    public static void testSetTabUserEvent() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/gameEventWar/cliente-SetTabUserEvent.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        SetTabUserEvent settabuserevent = (SetTabUserEvent) objLogin;
        Document xml = XMLSerializer.toXML(settabuserevent);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        SetTabUserEvent settabuserevent2 = (SetTabUserEvent) objLogin2;
        if (settabuserevent.equals(settabuserevent2)) {
            System.out.println("SetTabUserEvent OK\n");
        } else {
            System.out.println("SetTabUserEvent failed");
        }
    }
    public static void testUserRegistredEvent() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/gameEventWar/cliente-UserRegistredEvent.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        UserRegistredEvent userregistredevent = (UserRegistredEvent) objLogin;
        Document xml = XMLSerializer.toXML(userregistredevent);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        UserRegistredEvent userregistredevent2 = (UserRegistredEvent) objLogin2;
        if (userregistredevent.equals(userregistredevent2)) {
            System.out.println("UserRegistredEvent OK\n");
        } else {
            System.out.println("UserRegistredEvent failed");
        }
    }
    public static void testUserUnregistredEvent() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/gameEventWar/cliente-UserUnregistredEvent.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        UserUnregistredEvent userunregistredevent = (UserUnregistredEvent) objLogin;
        Document xml = XMLSerializer.toXML(userunregistredevent);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        UserUnregistredEvent userunregistredevent2 = (UserUnregistredEvent) objLogin2;
        if (userunregistredevent.equals(userunregistredevent2)) {
            System.out.println("UserUnregistredEvent OK\n");
        } else {
            System.out.println("UserUnregistredEvent failed");
        }
    }
    public static void testWarEvent() throws JDOMException, IOException, BadProtocolException {
        SAXBuilder builder = new SAXBuilder(); // PARCEADOR
        Document doc = builder.build("xmlSamples/gameEventWar/cliente-WarEvent.xml");
        XMLSerializable objLogin = XMLSerializer.fromXML(doc);
        WarEvent warevent = (WarEvent) objLogin;
        Document xml = XMLSerializer.toXML(warevent);
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(xml, System.out);
        XMLSerializable objLogin2 = XMLSerializer.fromXML(xml);
        WarEvent warevent2 = (WarEvent) objLogin2;
        if (warevent.equals(warevent2)) {
            System.out.println("WarEvent OK\n");
        } else {
            System.out.println("WarEvent failed");
        }
    }
    public static void main(String[] args) throws JDOMException, IOException, BadProtocolException {
        // testLoginRequest(); //Listo!!!
        // testLoginResponse(); //Listo!!!
        // testStandardRequest(); //ListTableRequest //Pendiente de douglas
        // testListTableResponse(); //Listo!!!
        // testStartGameRequest(); //Listo!!!
        // testStartGameResponse(); //Listo!!!
         testJoinRequest(); //Listo!!!
        // testJoinResponse(); // Listo!!!
        // testCreateResponse(); //Listo!!!
        // testCreateRequest(); //Listo!!!
        //testSetBoardRequest(); //Listo!!!
        //testSetBoardResponse();//Listo!!!
        //testMoveFichaRequest();//Listo!!!
    	//testEndGameEvent();//Listo!!!
    	//testEndTimeEvent();//Listo!!!
    	//testMoveTabEvent();
    	//testPlayUserEvent();
    	//testSetBoardEvent();
    	//testSetTabUserEvent();
    	//testUserRegistredEvent();
    	//testUserUnregistredEvent();
    	//testWarEvent();
    }
}
// SAXBuilder -> PARCEADOR
